package cn.tedu.service;

import cn.tedu.pojo.User;
import cn.tedu.util.JsonUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class UserServiceImpl implements UserService{
    @Value("${sp.user-service.users}")
    private String usersJson;

    @Override
    public User getUser(Integer id) {
        log.info("输出日志信息");
        //使用匿名内部类的格式来写中间的类型格式转换
        List<User> users = JsonUtil.from(usersJson, new TypeReference<List<User>>(){});
        for(User user:users){
            if (user.getId().equals(id)){
                return user;
            }
        }
        return new User(id,"name-"+id,"pwd-"+id);
    }

    @Override
    public void addScore(Integer id, Integer score) {
        log.info("新增积分");
    }
}
